package com.zjw.zy.componse.demo.fragment

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.lifecycle.viewmodel.compose.viewModel
import com.zjw.zy.componse.demo.R
import com.zjw.zy.componse.demo.base.BaseComposeFragment
import com.zjw.zy.componse.demo.ui.theme.MyApplicationTheme
import com.zjw.zy.componse.demo.viewmodel.MineGameViewModel

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2023/2/17 10:27
 * @description：扫雷游戏
 */
class MineGameFragment : BaseComposeFragment() {

    companion object {
        private const val TAG = "GameFragment"
    }

    override fun onContentView(rootView: ComposeView) {
        rootView.setContent {
            MyApplicationTheme {
                ClearanceGame()
            }
        }
    }

    override fun onSubscribeValue() {

    }

    @Composable
    @Preview
    fun ClearanceGame(height: Int = 15, width: Int = 10) {
        Log.d(TAG, "ClearanceGame() called with: size = $height $width")
        if (height <= 0) {
            return
        }
        val vm: MineGameViewModel = viewModel()
        vm.initGame(height = height, width = width)
        val itemSize = remember {
            mutableStateOf(30.dp * (10f / width))
        }

        ConstraintLayout(
            modifier = Modifier
                .fillMaxSize()
                .padding(3.dp)
        ) {

            val (p1, p2, p3, p4, p5, p6) = remember {
                createRefs()
            }
            Text(text = "经典扫雷", modifier = Modifier.constrainAs(p1) {
                top.linkTo(parent.top)
                start.linkTo(parent.start)
                end.linkTo(parent.end)
            })

            if (vm.resultChange.value != MineGameViewModel.RESULT_NONE) {
                val resultText = if (vm.resultChange.value == MineGameViewModel.RESULT_SUCCESS) {
                    "恭喜，你赢啦！"
                } else {
                    "哈哈，你输啦！"
                }
                Text(text = resultText, modifier = Modifier.constrainAs(p5) {
                    top.linkTo(p1.top)
                    start.linkTo(parent.start)
                    end.linkTo(parent.end)
                    bottom.linkTo(p2.top)
                })
            }

//            Switch(
//                checked = vm.findFlag.value, onCheckedChange = {
//                    //发布 插旗模式
//                    vm.findFlag.value = it
//                },
//                modifier = Modifier.constrainAs(p6) {
//                    top.linkTo(parent.top)
//                    bottom.linkTo(p2.top)
//                    end.linkTo(parent.end)
//                }
//            )

            Column(modifier = Modifier
                .wrapContentSize()
                .constrainAs(p2) {
                    top.linkTo(p1.bottom)
                    bottom.linkTo(parent.bottom)
                    start.linkTo(parent.start)
                    end.linkTo(parent.end)
                }) {
                for (i in 0 until height) {
                    Row(
                        modifier = Modifier
                            .height(itemSize.value)
                            .wrapContentWidth()
                    ) {
                        for (j in 0 until width) {
                            GameItem(size = itemSize.value, vm.observeItem(j, i)) { flag ->
                                vm.performClickItem(j, i, flag)
                            }
                        }
                    }
                }
            }

            Text(text = vm.timeChange.value.toString(), modifier = Modifier.constrainAs(p3) {
                top.linkTo(parent.top)
                end.linkTo(parent.end)
            })

            if (vm.resultChange.value != MineGameViewModel.RESULT_NONE) {
                Button(
                    onClick = { vm.initGame(height = height, width = width) },
                    modifier = Modifier.constrainAs(p4) {
                        bottom.linkTo(parent.bottom)
                        start.linkTo(parent.start)
                        end.linkTo(parent.end)
                    },
                ) {
                    Text(text = "重新开始")
                }
            }
        }
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun GameItem(
        size: Dp,
        state: State<Int> = mutableStateOf(MineGameViewModel.ITEM_STATUS_UN_CLICK),
        onClick: (flag: Boolean) -> Unit
    ) {

        Box(modifier = Modifier
            .size(size)
            .combinedClickable(
                onClick = { onClick(false) },
                onLongClick = { onClick(true) }
            )) {

            Image(
                painter = painterResource(
                    id = when (state.value) {
                        MineGameViewModel.ITEM_STATUS_BANG -> {
                            R.drawable.bg_src_game_item_status_1
                        }
                        MineGameViewModel.ITEM_STATUS_UN_CLICK -> {
                            R.drawable.bg_src_game_item_status_0
                        }
                        MineGameViewModel.ITEM_STATUS_FLAG -> {
                            R.drawable.bg_src_game_item_status_2
                        }
                        MineGameViewModel.ITEM_STATUS_BLACK -> {
                            R.drawable.bg_src_game_item_status_3
                        }
                        else -> {
                            R.drawable.bg_src_game_item_status_3
                        }
                    }
                ), contentDescription = null
            )

            if (state.value > 0) {
                Text(
                    text = state.value.toString(),
                    modifier = Modifier.align(Alignment.Center),
                    textAlign = TextAlign.Center,
                )
            }

        }
    }
}